//加载js
function loadJs(jspath){
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = jspath;
    document.body.appendChild(script);
}

/**
 * 1.二级页面首页
 * 2.直播列表
 * 3.专家列表
 * 4.留言列表
 * 5.发帖提问列表
 * 6.发帖
 * 7.在线诊断
 * 8.病虫害查询
 * 9.专家指导
 * 10.农事提示
 * 11.防治案例
 * 12.个人中心
 * 13.常见病虫害
 */
var crop_id = "";
var flag=$("#flag").val();
if(flag=="" || flag == "1"){
    toIndex();
}else if(flag == "2"){
    liveList();
}else if(flag == "3"){
    expertList();
}else if(flag == "4"){
    leaveList();
}else if(flag == "5"){
    pasteList();
}else if(flag == "6"){
    addPaste();
}else if(flag == "7"){
    pestDiagnosis();
}else if(flag == "8"){
    pestSearch();
}else if(flag == "9"){
    expertGuide();
}else if(flag == "10"){
    farmingHint();
}else if(flag == "11"){
    treatmentCases();
}else if(flag == "12"){
    center();
}else if(flag == "13"){
    commonPest();
}else{
    toIndex();
}
//二级页面首页
function toIndex() {
    // window.location.href="/pestdiagnosis/index";
    $("#mainDiv").load("/pestdiagnosis/u/toCommon","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });

}
// 直播列表
function liveList() {
    $("#mainDiv").load("/pestdiagnosis/u/liveList","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}

function toZBDetail(url) {
    var add = encodeURI(encodeURI(url));
    // window.location.href="/pestdiagnosis/u/toLiveDetail?url="+add;
    window.open("/pestdiagnosis/u/toLiveDetail?url="+add);
}

//专家列表
function expertList() {
    $("#mainDiv").load("/pestdiagnosis/u/expertList","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}

//疑难交流
function ynPaste() {
    $("#mainDiv").load("/pestdiagnosis/u/ynPaste","",function(response,status,xhr){
        loadJs(rootPath+"js/ueditor/ueditor.js");
        UE.delEditor('editor');
        var ue = UE.getEditor('editor');
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}

//收藏列表
function scList() {
    $("#mainDiv").load("/pestdiagnosis/u/sclist?ids="+$.cookie("ids"),"",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}

//开始咨询
function startZx(id,name) {
    $("#zjName").text(name);
    $("#zj_id").val(id);
    $("#zj_name").val(name);
    $("#charBox").show();
}
function endZx() {
    $("#charBox").hide();
    $('.newsList').html("");
}
//存放消息id
var ids = new Array();
//发送
function sendMes() {
    var content = $("#dope").val();
    if(content == ""){
        alert("不能为空");
        return false;
    }
    send(
        '/pestdiagnosis/u/sendMes',
        {content: content,expertId:$("#zj_id").val()},
        function (data) {
            if(data.result == "0"){
                //alert(data.message);
                $('#dope').val('');
                var str='';
                str+='<li>'+
                    '<div class="nesHead"><img src="/pestdiagnosis/images/user/img/6.jpg"/></div>'+
                    '<div class="news"><img class="jiao" src="/pestdiagnosis/images/user/img/20170926103645_03_02.jpg">'+content+'</div>'+
                    '</li>';
                $('.newsList').append(str);
                ids.push(data.data);
            }
        },
        function (res) {
            alert("网络异常，请稍后再试");
        }
    );
}
queryMes();
setInterval(function(){
    queryMes();
},5000);

Array.prototype.indexOf = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

function queryMes() {
    if(ids.length > 0){
        var id = ids.join(",");
        send(
            "/pestdiagnosis/u/queryMes",
            {ids: id},
            function (data) {
                var list = data.data;
                for(var i=0;i<list.length;i++){
                    ids.remove(list[i].id);
                    var answer='';
                    answer+='<li>'+
                        '<div class="answerHead"><img src="/pestdiagnosis/images/user/img/tou.jpg"/></div>'+
                        '<div class="answers"><img class="jiao" src="/pestdiagnosis/images/user/img/jiao.jpg">'+list[i].replyContent+'</div>'+
                        '</li>';
                    $('.newsList').append(answer);
                }
            },
            function (res) {
                alert("网络异常，请稍后再试");
            }
        );
    }
}

//留言列表
function leaveList() {
    $("#mainDiv").load("/pestdiagnosis/u/leaveList","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//发帖提问列表
function pasteList() {
    $("#mainDiv").load("/pestdiagnosis/u/pasteList","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//发帖
function addPaste2() {

    $("#mainDiv").load("/pestdiagnosis/u/addPaste","",function(response,status,xhr){
        // loadJs(rootPath+"js/ueditor/ueditor.config.js");
        // loadJs(rootPath+"js/ueditor/ueditor.all.js");
        // loadJs(rootPath+"js/ueditor/lang/zh-cn/zh-cn.js");
        loadJs(rootPath+"js/ueditor/ueditor.js");
        // UE.getEditor('editor');

        UE.delEditor('editor');
        var ue = UE.getEditor('editor');
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}

//在线诊断
function pestDiagnosis() {
    $("#mainDiv").load("/pestdiagnosis/u/pestDiagnosis","",function(response,status,xhr){
        var dt = $("#c5").val();
        var sc = $("#c6").val();
        var gs = $("#c7").val();
        if(dt && dt != "0"){
            $("#sel_crop_type").val(1);
            cropTypeOnchange(1);
            $("#sel_crop").val(dt);
        }else if(sc && sc != "0"){

            $("#sel_crop_type").val(2);
            cropTypeOnchange(2);
            $("#sel_crop").val(sc);
        }else if(gs && gs != "0"){

            $("#sel_crop_type").val(3);
            cropTypeOnchange(3);
            $("#sel_crop").val(gs);
        }
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//病虫害查询
function pestSearch() {
    $("#mainDiv").load("/pestdiagnosis/u/pestSearch","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
        var c1 = $("#c1").val();//作物分类
        var c2 = $("#c2").val();//作物id
        var c3 = $("#c3").val();//病害分类
        var c4 = $("#c4").val()//病害id

        if(c1){
            $("#sel_crop_type").val(c1);//有值

            getCropByid(c1);//获取作物

            $("#sel_crop").val(c2);//作物选中
            crop_id = c2;
            $("#sel_disease_type").val(c3);//有值
            disease_type_onchange2(c3);//获取名称

            setTimeout(function () {
                $("#sel_disease_name").val(c4);
                pestSearch2();//查询
            },500);

            // $("input[name='chk_bhmc'][value='"+c4+"']").prop("checked",true);


        }else{
            $("[name=sel_kong]").show();
            $("[name=sel_no_kong]").hide();
        }

    });
}
//专家指导
function expertGuide() {
    $("#mainDiv").load("/pestdiagnosis/u/expertGuide","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//农事提示
function farmingHint() {
    $("#mainDiv").load("/pestdiagnosis/u/farmingHint","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//防治案例
function treatmentCases() {
    $("#mainDiv").load("/pestdiagnosis/u/treatmentCases","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//个人中心
function center() {
    $("#mainDiv").load("/pestdiagnosis/u/center","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//常见病虫害
function commonPest() {
    $("#mainDiv").load("/pestdiagnosis/u/commonPest","",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}

//分类onchange事件
function cropTypeOnchange(id) {
    getCropByid(id);
}

function getCropByid(id) {
    send(
        "/pestdiagnosis/u/getCropById",
        {id: id},
        function (data) {
            if(data.result == 0){
                var list = data.data;
                $("#sel_crop").empty();
                $("#sel_crop").append("<option value='0'>请选择作物名称</option>");
                for(var i=0;i<list.length;i++){
                    $("#sel_crop").append("<option value='"+list[i].id+"'>"+list[i].cropName+"</option>");
                }
            }else{
                alert(data.messge);
            }
        },
        function (res) {
            alert("网络异常，请稍后再试")
        }
    )
}
//选择作物名称onchange

function crop_onchange(id) {
    crop_id = id;
    $("[name=sel_kong]").hide();
    $("[name=sel_no_kong]").show();
    getDiseaseType();

}

//获取病害类型
function getDiseaseType() {
    send(
        "/pestdiagnosis/u/getDiseaseType",
        {},
        function (data) {
            var list = data.data;
            $("#sel_disease_type").empty();
            $("#sel_disease_type").append("<option value='0'>请选择病害类型</option>");
            for(var i=0;list.length;i++){
                $("#sel_disease_type").append("<option value='"+list[i].id+"'>"+list[i].typeName+"</option>");
            }
        },
        function (res) {
            alert("网络异常，请稍后再试")
        }
    )
}

function disease_type_onchange(id) {
    if (crop_id == "") {
        alert("请选择作物名称");
        return false;
    } else {
        send(
            "/pestdiagnosis/u/getDiseaseName",
            {cropId: crop_id, diseaseTypeId: id},
            function (data) {
                var list = data.data;
                $("#sel_disease_name").empty();
                $("#sel_disease_name").append("<option value='0'>请选择病害名称</option>");
                for (var i = 0; i < list.length; i++) {
                    $("#sel_disease_name").append("<option value='" + list[i].id + "'>" + list[i].name + "</option>");
                }
            },
            function (res) {
                alert("网络异常，请稍后再试");
            }
        );
    }
}
//二级页面用
function disease_type_onchange2(id) {
    if (crop_id == "") {
        alert("请选择作物名称");
        return false;
    } else {
        send(
            "/pestdiagnosis/u/getDiseaseName",
            {cropId: crop_id, diseaseTypeId: id},
            function (data) {
                var list = data.data;
                $("#sel_disease_name").empty();
                $("#sel_disease_name").append("<option value='0'>请选择病害名称</option>");
                // $("#bhmc_chk").html("");
                for (var i = 0; i < list.length; i++) {
                     $("#sel_disease_name").append("<option value='" + list[i].id + "'>" + list[i].name + "</option>");
                    // $("#bhmc_chk").append("<input type='checkbox' value='"+list[i].id+"' id='"+list[i].id+"'><label for='"+list[i].id+"'>"+list[i].name+"</label>");
                    //$("#bhmc_chk").append("<input type='checkbox' name='chk_bhmc' value='"+list[i].id+"' id='"+list[i].id+"b'><label style='position: static;' th:for='"+list[i].id+"b'>"+list[i].name+"</label>");
                }
            },
            function (res) {
                alert("网络异常，请稍后再试");
            }
        );
    }
}
//开始查询
function startSearch() {
    var cropTypeId = $("#sel_crop_type").val();
    var cropId = $("#sel_crop").val();
    var typeId = $("#sel_disease_type").val();
    var typeNameId = $("#sel_disease_name").val();
    // alert(cropTypeId+"  "+cropId+"   "+typeId+"   "+typeNameId);
    if(cropTypeId == "0"){
        alert("请选择作物类别");
        return false;
    }else if(cropId == "0"){
        alert("请选择作物名称");
        return false;
    }else if(typeId == "0"){
        alert("请选择病害类型");
        return false;
    }else if(typeNameId == "0"){
        alert("请选择病害名称");
        return false;
    }
    window.location.href="/pestdiagnosis/u/second?flag=8&cropTypeId="+cropTypeId+"&cropId="+cropId+"&typeId="+typeId+"&typeNameId="+typeNameId;
}

function startSearch2() {
    var dtzwId = $("#sel_dtzw").val();
    var sczwId = $("#sel_sczw").val();
    var gszpId = $("#sel_gszp").val();
    var flag = false;
    if(dtzwId != "0" && sczwId == "0" && gszpId == "0"){
        flag = true;
    }else if(dtzwId == "0" && sczwId != "0" && gszpId == "0"){
        flag = true;
    }else if(dtzwId == "0" && sczwId == "0" && gszpId != "0"){
        flag = true;
    }
    if(flag){
        window.location.href="/pestdiagnosis/u/second?flag=7&dtzwId="+dtzwId+"&sczwId="+sczwId+"&gszpId="+gszpId;
    }else{
        alert("只可以选择一个");
        return false;
    }
}
//发送留言
function send_msg() {
    var l_name = $("#l_name").val();
    var l_expert = $("#l_expert").val();
    var l_expert_name = $("#l_expert").find("option:selected").text();
    var l_bch_type = $("#l_bch_type").val();
    var l_bch_type_name = $("#l_bch_type").find("option:selected").text();
    var l_phone = $("#l_phone").val();
    var l_message = $("#i_message").val();
    if(l_name == "" || l_expert == "0" || l_bch_type == "0" || l_phone == "" || l_message == ""){
        alert("请检查信息是否正确");
        return false;
    }
    send(
        "/pestdiagnosis/u/addLeaveMessage",
        {leaveUserNick: "游客",telphone:l_phone,consultExpertId: l_expert,consultExpertName: l_expert_name,pestType: l_bch_type, leaveContent: l_message},
        function (data) {
            if(data.result == "0"){
                alert("发送成功");
                window.location.reload();
            }else{
                alert(data.message);
            }
        },
        function (res) {
            alert("网络异常，请稍后再试");
        }
    );

}

function showMsg(msg) {
    var m = msg=="null" ?"尚未回答":msg;
    layer.open({
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['420px', '240px'], //宽高
        content: m
    });
}
//专家查询
function expertSearch() {
    var name = $("#expert_name").val();
    var typeId = $("#sel_type").val();
    var url = encodeURI("/pestdiagnosis/u/expertList?expertName="+name+"&typeId="+typeId);
    $("#mainDiv").load(url,"",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//留言查询
function leaveSearch() {
    var name = $("#expert_name").val();
    var typeId = $("#sel_type").val();
    var url = encodeURI("/pestdiagnosis/u/leaveList?expertName="+name+"&typeId="+typeId);
    $("#mainDiv").load(url,"",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//发帖提问查询
function pasteSearch() {
    var name = $("#name").val();
    var typeId = $("#sel_type").val();
    var url = encodeURI("/pestdiagnosis/u/pasteList?name="+name+"&typeId="+typeId);
    $("#mainDiv").load(url,"",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//防治案例查询
function fzanSearch() {
    var kName = $("#k_name").val();
    var url = encodeURI("/pestdiagnosis/u/treatmentCases?kName="+kName);
    $("#mainDiv").load(url,"",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//专家指导查询
function zjzdSearch() {
    var kName = $("#k_name").val();
    var url= encodeURI("/pestdiagnosis/u/expertGuide?kName="+kName);
    $("#mainDiv").load(url,"",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//农事提示查询
function nstsSearch() {
    var kName = $("#k_name").val();
    var url = encodeURI("/pestdiagnosis/u/farmingHint?kName="+kName);
    $("#mainDiv").load(url,"",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//常见病虫害查询
function cjbchSearch() {
    var kName = $("#k_name").val();
    var url = encodeURI("/pestdiagnosis/u/commonPest?kName="+kName);
    $("#mainDiv").load(url,"",function(response,status,xhr){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//病虫害查询
function pestSearch2() {
    var cropTypeId = $("#sel_crop_type").val();
    var cropId = $("#sel_crop").val();
    var typeId = $("#sel_disease_type").val();
    var typeNameId = $("#sel_disease_name").val();
    // $("input[name='chk_bhmc']:checked").each(function(){
    //     if(typeNameId==""){
    //         typeNameId=$(this).val();
    //     }else{
    //         typeNameId+=","+$(this).val();
    //     }
    // });
    // alert(cropTypeId+"  "+cropId+"   "+typeId+"   "+typeNameId);
    if(cropTypeId == "0"){
        alert("请选择作物类别");
        return false;
    }else if(cropId == "0"){
        alert("请选择作物名称");
        return false;
    }else if(typeId == "0"){
        alert("请选择病害类型");
        return false;
    }else if(typeNameId == "0"){
        alert("请选择病害名称");
        return false;
    }
    $("#mainDiv").load("/pestdiagnosis/u/pestSearch?cropId="+cropId+"&typeNameId="+typeNameId,"",function(response,status,xhr){
        var c1 = $("#c1").val();//作物分类
        var c2 = $("#c2").val();//作物id
        var c3 = $("#c3").val();//病害分类
        var c4 = $("#c4").val()//病害id

        // if(c1){
            $("#sel_crop_type").val(cropTypeId);//有值

            getCropByid(cropTypeId);//获取作物

            $("#sel_crop").val(cropId);//作物选中
            crop_id = cropId;
            $("#sel_disease_type").val(typeId);//有值
            disease_type_onchange(typeId);//获取名称


            disease_type_onchange2(typeId);//获取名称

            setTimeout(function () {
                $("#sel_disease_name").val(typeNameId);
            },500);




            // var arr = typeNameId.split(",");
            // for(var i=0;i<arr.length;i++){
            //     $("input[name='chk_bhmc'][value='"+arr[i]+"']").prop("checked",true);
            // }

        // }
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}
//在线诊断
function diagnosisSearch() {
    var sel_crop_type = $("#sel_crop_type").val();
    var sel_da = $("#sel_da").val();
    var sel_crop = $("#sel_crop").val();
    var sel_dt = "";

    $("input[name='bhtx_chk']:checked").each(function(){
        if(sel_dt==""){
            sel_dt=$(this).val();
        }else{
            sel_dt+=","+$(this).val();
        }
    });

    if(sel_crop_type == "0"){
        alert("请选择作物分类");
        return false;
    }else if(sel_da == "0"){
        alert("请选择病害部位");
        return false;
    }else if(sel_crop == "0"){
        alert("请选择作物名称");
        return false;
    }
    // else if(sel_dt == ""){
    //     alert("请选择病害特性");
    //     return false;
    // }
    $("#mainDiv").load("/pestdiagnosis/u/pestDiagnosis?cropId="+sel_crop+"&daId="+sel_da+"&dtId="+sel_dt,"",function(response,status,xhr){
        $("#sel_crop_type").val(sel_crop_type);
        cropTypeOnchange(sel_crop_type);
        $("#sel_da").val(sel_da);
        $("#sel_crop").val(sel_crop);
        // $("#sel_dt").val(sel_dt);
        var arr = sel_dt.split(",");
        for(var i=0;i<arr.length;i++){
            $("input[name='bhtx_chk'][value='"+arr[i]+"']").prop("checked",true);
        }
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    });
}

//发帖
function addPaste() {
    var name = $("#name").val();
    var typeId = $("#sel_type").val();
    var typeName = $("#sel_type").find("option:selected").text();
    var content = UE.getEditor('editor').getContent();
    if(name == ""){
        alert("请输入名称");
        return false;
    }else if(typeId == "0"){
        alert("请选择类型");
    }else if(typeName == ""){
        alert("请选择类型");
    }else if(content == ""){
        alert("请输入内容");
        return false;
    }
    send(
        '/pestdiagnosis/u/add_paste',
        {name:name,typeId:typeId,typeName:typeName,content:content},
        function (data) {
            if(data.result == "0"){
                alert("添加成功");
                window.location.reload();
            }else{
                alert(data.message);
            }
        },
        function (res) {
            alert("网络异常，请稍后再试");
        }
    );

}
//收藏
function sc(id) {
    var ids = $.cookie("ids");
    if(ids == null || ids == "" || typeof(ids) == 'undefined') {
        $.cookie("ids", id, {expires: 365, path: '/'});
    }else{
        var n_ids = ids+","+id;
        $.cookie("ids",n_ids , {expires: 365, path: '/'});
    }
    alert("收藏成功");
}
//取消收藏
function qxsc(id) {
    $("#"+id).hide();
    var ids = $.cookie("ids");
    if(ids != null && ids != "" && typeof(ids) != 'undefined') {
        var arr = ids.split(",");
        for(var i=0;i<arr.length;i++){
            if(arr[i] == id){
                arr.splice($.inArray(id,arr),1);
                i=0;
            }
        }
        $.cookie("ids",arr.join(",") , {expires: 365, path: '/'});
    }
}


function zj(id,dfl) {

    send(
        '/pestdiagnosis/u/getExpertInfo',
        {id: id},
        function (data) {
            var d = data.data;
            var res = "";
            res+='<div class="col-sm-12 col-md-12 product-grid">';
            res+='<div class="thumbnail">';
            res+='<img src="'+d.headImgUrl+'" style="width: 100%;height: 200px;" alt="...">';
            res+='<div class="caption">';
            res+='<small style="display:none;">Category</small>';
            res+='<small class="pull-right" style="display:none;">';
            res+='<span class="rate fa-star fa"></span>';
            res+='<span class="rate fa-star fa"></span>';
            res+='<span class="rate fa-star fa"></span>';
            res+='<span class="rate fa-star fa"></span>';
            res+='<span class="rate fa-star-half fa"></span>';
            res+='</small>';
            res+='<h4>'+d.name+'</h4>';
            res+='<p>答复率:'+dfl+'</p>';
            res+='<p>职位:'+d.position+'</p>';
            res+='<p>介绍:'+d.introduction+'</p>';
            res+='</div>';
            res+='</div>';
            res+='</div>';
            layer.open({
                type: 1,
                skin: 'layui-layer-rim', //加上边框
                area: ['300px', '500px'], //宽高
                content: res
            });
        },
        function (res) {

        }
    );


}

addIp();
function addIp() {
    send(
        '/pestdiagnosis/u/addIp',
        {ip: returnCitySN["cip"]},
        function (data) {
            console.log("add ip succ");
        },
        function (res) {

        }
    );
}